//----------------------------------------------------------------------
//
//	XFILE, XFILEREAD, XFILEWRITE
//
//	output classes that read and write compressed files
//
//----------------------------------------------------------------------
#ifndef XFILE_HPP
#define XFILE_HPP

#define COMPRESSBUFFERSIZE		(4*1024)

//----------------------------------------------------------------------
//	forward reference for main zlib structure 
//----------------------------------------------------------------------
typedef struct z_stream_s z_stream;

//----------------------------------------------------------------------
//	XFILE base class
//----------------------------------------------------------------------
class XFILE
{
public:
	XFILE() :	hFile(INVALID_HANDLE_VALUE), pZlib(0),
				compressBuffer(0),
				compressBufferSize(COMPRESSBUFFERSIZE)
	{}
	virtual	~XFILE();

	virtual	bool	Open( const char *filename ) = 0;
	virtual void	Close();

	// is the file open?
	virtual	bool	IsOpen();

	// get the number of bytes written or read
	virtual u32		GetTrafficBytes( bool compressed=false ) = 0;

protected:
			bool	CompressInit( bool init=true );
	virtual	bool	CompressSetup( bool init=true ) = 0;
	virtual bool	DoIO();
	virtual	void	Flush();

protected:

	HANDLE		hFile;				// file pointer
	z_stream	*pZlib;				// pointer to zlib
	u32			compressBufferSize;	// size of compression buffer
	u8			*compressBuffer;	// pointer to compression buffer
};

//----------------------------------------------------------------------
//	XFILEREAD class
//	reading from a compressed file
//----------------------------------------------------------------------
class XFILEREAD : public XFILE
{
public:
	XFILEREAD();
	~XFILEREAD();

			bool	Open( const char *filename );
			u32		GetTrafficBytes( bool compressed=false );
	virtual	bool	Read( void *ptr, u32 numBytes, u32 *numBytesRead=0 );
	virtual	bool	Skip( u32 numBytes, u32 *numBytesRead=0 );

protected:
			bool	CompressSetup( bool init=true );
			bool	DoIO();
};

//----------------------------------------------------------------------
//	XFILEWRITE class
//	writing to a compressed file
//----------------------------------------------------------------------
class XFILEWRITE : public XFILE
{
public:
	XFILEWRITE();
	~XFILEWRITE();

			bool	Open( const char *filename );
			u32		GetTrafficBytes( bool compressed=false );
	virtual	bool	Write( const void *ptr, u32 numBytes, u32 *numBytesWritten=0 );
	virtual	bool	WriteByte( u8 byteToWrite, u32 numBytes, u32 *numBytesWritten=0 );

protected:
			bool	CompressSetup( bool init=true );
			bool	DoIO();
			void	Flush();
};

#endif
